﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ScratchNet
{
    /// <summary>
    /// 延时
    /// </summary>
    public class DelayExpression : Expression
    {
        public override string ReturnType => "any";

        public DelayExpression()
        {
            
        }
        public Expression Value { get; set; }

        protected override Completion ExecuteImpl(ExecutionEnvironment enviroment)
        {
            var c = Value.Execute(enviroment);
            if (!c.IsValue)
                return c;
            c.ExecStatementStr = Sleep(c.ReturnValue);
            return c;
        }
        public override Descriptor Descriptor
        {
            get
            {
                Descriptor desc = new Descriptor();
                desc.Add(new TextItemDescriptor(this, "EDS2000 延时"));
                desc.Add(new ExpressionDescriptor(this, "Value", "any"));
                desc.Add(new TextItemDescriptor(this, "毫秒"));

                return desc;
            }
        }

        public override string Type
        {
            get { return "DelayExpression"; }
        }

        private string Sleep(object val)
        {
            //调接口
            return ($"DelayExpression.Sleep({val});");
        }
    }
}
